package core

import (
	"sync"
)

// Cache 层，减少频繁 IO
type Cache struct {
	data  map[string]*Task
	mutex sync.RWMutex
}

func NewCache() *Cache {
	return &Cache{
		data: make(map[string]*Task),
	}
}

func (c *Cache) Get(taskID string) (*Task, bool) {
	c.mutex.RLock()
	defer c.mutex.RUnlock()
	task, ok := c.data[taskID]
	return task, ok
}

func (c *Cache) Set(task *Task) {
	c.mutex.Lock()
	defer c.mutex.Unlock()
	c.data[task.ID] = task
}

func (c *Cache) Delete(taskID string) {
	c.mutex.Lock()
	defer c.mutex.Unlock()
	delete(c.data, taskID)
}
